#include "homepage.h"
#include "ui_homepage.h"
#include <QDebug>
#define dbg qDebug()

homePage::homePage(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::homePage)
{
    ui->setupUi(this);
    dev_t.devsta = false;
    dev_t.ledsta = 0;
    memset(dev_t.shidu,'\0', sizeof (dev_t.shidu));
    memset(dev_t.wendu,'\0', sizeof (dev_t.wendu));


    player = new QMediaPlayer(this);
    player->setVolume(100);

    mysocket = new QTcpSocket(this);
    mysocket->connectToHost(QHostAddress("127.0.0.1"),8888);

    //注册连接处理
    connect(mysocket,&QTcpSocket::connected,
            [=](){
        //向服务器注册本机信息
        mysocket->write("CLIENT");
        //注册数据处理
        connect(mysocket,&QTcpSocket::readyRead,
                [=](){
            {
                dev_t.devsta = true;
                QByteArray arry = mysocket->readAll();
                    dbg<<arry;
                if(arry.startsWith("WD")){
                    ui->dialog->append("请注意防盗");
                    QUrl url("qrc:/mp3/mp3/00.mp3");

                    player->setMedia(url);

                    player->play();
                }
                else if(arry.startsWith("WF")){
                    ui->dialog->append("检测到火焰");
                    QUrl url("qrc:/mp3/mp3/01.mp3");
                    player->setMedia(url);
                    player->play();
                }
                else
                {
                     dev_t.ledsta = strstr(QString(arry).section("##",0,0).toUtf8().data(),"ON") ?1:0;

                     sprintf(dev_t.wendu,"温度:%s℃",QString(arry).section("##",1,1).toUtf8().data());
                     sprintf(dev_t.shidu,"湿度:%s%%",QString(arry).section("##",2,2).toUtf8().data());
                     if(dev_t.ledsta)
                     {
                         ui->lightbt->setStyleSheet("border-image:  url(:/pic/door_on/s2.png);");
                         ui->label_led->setText("灯：已打开");
                     }
                     else
                     {
                         ui->lightbt->setStyleSheet("border-image:  url(:/pic/door_on/s1.png);");
                         ui->label_led->setText("灯：已关闭");
                     }
                     ui->label_wendu->setText(dev_t.wendu);
                     ui->label_shidu->setText(dev_t.shidu);
                }
            }
        }
        );
        //注册断开处理
        connect(mysocket,&QTcpSocket::disconnectFromHost,
                [=](){
            dev_t.devsta = false;
        }
        );

    }
    );


}


homePage::~homePage()
{
    delete ui;
}

#if 1
void homePage::paintEvent(QPaintEvent *)
{
    QPixmap img;
    img.load(":/pic/bg1.jpg");
    img = img.scaled(this->width(),this->height(),Qt::IgnoreAspectRatio);
    QPainter p(this);
    p.drawPixmap(0,0,img);
}
#endif

QPixmap homePage::resizPic(QString path, int position)
{
    //设置新的图片的透明度
    QPixmap pix1_(path);

    QPixmap temp(pix1_.size());
    temp.fill(Qt::transparent);

    QPainter p1(&temp);
    p1.setCompositionMode(QPainter::CompositionMode_Source);
    p1.drawPixmap(0, 0, pix1_);
    p1.setCompositionMode(QPainter::CompositionMode_DestinationIn);

    //根据QColor中第四个参数设置透明度，此处position的取值范围是0～255
    p1.fillRect(temp.rect(), QColor(0, 0, 0, position));
    p1.end();

    pix1_ = temp;
    return pix1_;
}

void homePage::on_lightbt_clicked()
{

    if(dev_t.devsta)
    {
        QString tp = getTime();
        if(dev_t.ledsta)
        {
            dev_t.ledsta = false;
            ui->lightbt->setStyleSheet("border-image:  url(:/pic/door_on/s1.png);");
            tp.append("已关灯");
             ui->label_led->setText("灯：已关闭");
             mysocket->write("SEND+LEDOFF+END");
             QUrl url("qrc:/mp3/mp3/11.mp3");
             player->setMedia(url);
             player->play();

        }
        else {
            dev_t.ledsta = true;
            ui->lightbt->setStyleSheet("border-image:  url(:/pic/door_on/s2.png);");
            tp.append("已开灯");
             ui->label_led->setText("灯：已打开");
                mysocket->write("SEND+LEDON+END");
                QUrl url("qrc:/mp3/mp3/10.mp3");
                player->setMedia(url);
                player->play();

        }
        ui->dialog->append(tp);
    }

}

void homePage::on_beep_bt_clicked()
{
    if(dev_t.devsta)
    {
        QString tp = getTime();
        mysocket->write("SEND+BEEP+END");
        tp.append("已响铃");
        ui->dialog->append(tp);
        QUrl url("qrc:/mp3/mp3/20.mp3");
        player->setMedia(url);
        player->play();

    }
}

QString homePage::getTime()
{
    time_t t = time( nullptr );
    char tmp[64];
    strftime( tmp, sizeof(tmp), "%Y/%m/%d %X: ",localtime(&t) );
    return  QString("%1").arg(tmp);
}

void homePage::on_imag_bt_clicked()
{
    if(dev_t.devsta)
    {
        QString tp = getTime();
        mysocket->write("SEND+DOOR+END");
        tp.append("已开门");
        ui->dialog->append(tp);
        QUrl url("qrc:/mp3/mp3/21.mp3");
        player->setMedia(url);
        player->play();

    }
}

